package com.orange.base.component.biztool.simulateddata.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.orange.base.component.biztool.simulateddata.service.FileInfo;
import com.orange.base.component.biztool.simulateddata.service.SimulatedDataEnum;
import com.orange.base.component.biztool.simulateddata.service.SimulatedDataParam;

/**
 * 文件
 *
 * @author biankudingcha
 * @date: 2022-08-24
 * @since 0.5.0
 */
public class FileStrategy extends AbstractSimulatedData {

    public static final String[] WORD = { "《围炉夜话》全文附译文.doc", "菜根谭.doc", "诗经全文.doc", "小窗幽记.doc", "增广贤文.doc",
        "科技论文模板.doc" };

    public static final String[] EXCEL = { "2020全国成人高等学校名单-教育部.xls", "2020全国普通高等学校名单-教育部.xls", "2021年日历电子版(打印版).xlsx" };

    public static final String[] PDF = { "《围炉夜话》全文附译文.pdf", "Java huashan.pdf", "增广贤文.pdf" };

    public static final String[] PPT = { "《原则》.ppt", "论文PPT模板.ppt" };

    public static final String[] TXT = { "《小窗幽记》原文译文对照版.txt", "菜根谭.txt", "诗经.txt", "围炉夜话.txt", "增广贤文.txt" };

    public static final Map<String, String[]> fileTypeMap = new HashMap<String, String[]>();

    static {
        fileTypeMap.put("word", WORD);
        fileTypeMap.put("excel", EXCEL);
        fileTypeMap.put("pdf", PDF);
        fileTypeMap.put("ppt", PPT);
        fileTypeMap.put("txt", TXT);
    }

    public String getType() {
        return SimulatedDataEnum.FILE.getName();
    }

    public String getCaption() {
        return SimulatedDataEnum.FILE.getCaption();
    }

    @Override
    public Object getValue(SimulatedDataParam param) {
        String max = param.getMax();
        if (isEmpty(max)) {
            max = "1";
        }
        int length = toInt(max);
        String fileType = param.getFileType();
        if (isEmpty(fileType)) {
            fileType = "jpg";
        }
        String[] fileTypes = fileType.split(",");
        int j = 0;
        List<FileInfo> fileInfos = new ArrayList<FileInfo>();
        for (int i = 0; i < length; i++) {
            String type = fileTypes[j];
            j++;
            if (j == fileTypes.length) {
                j = 0;
            }
            InputStream is = null;
            String fileName = "";
            if (fileTypeMap.containsKey(type)) {
                String[] array = fileTypeMap.get(type);
                int index = getRandomNumber(0, array.length - 1);
                fileName = array[index];
                is = FileStrategy.class.getClassLoader().getResourceAsStream("/data/" + type + "/" + array[index]);
                if (is == null) {
                    is = FileStrategy.class.getResourceAsStream("/data/" + type + "/" + array[index]);
                }
            } else if ("jpg".equals(type)) {
                int index = getRandomNumber(1, 89);
                is = FileStrategy.class.getClassLoader().getResourceAsStream("/data/" + type + "/" + index + ".jpg");
                if (is == null) {
                    is = FileStrategy.class.getResourceAsStream("/data/" + type + "/" + index + ".jpg");
                }
                fileName = index + ".jpg";
            }
            if (is == null) {
                continue;
            }
            FileInfo fileInfo = new FileInfo(fileName, is);
            fileInfos.add(fileInfo);
        }
        return fileInfos;
    }

}
